<template>
	<!--表格单选-->
	<el-row class="brand">
  		<el-col :span="24">
  			<!--查询过滤条件-->
  			<el-form :inline="true" :model="conditionForm" ref="conditionForm" label-width="82px">
  				<el-form-item label="名称">				
					<m-input-auto-comp :inputData="conditionForm" :queryInterUrl="urlPaths.queryList" queryParamName="name"/>
				</el-form-item>
				<el-button type="primary" size="small" icon="search" @click="queryTableData">查询</el-button>
			</el-form>
  		</el-col>
  		<el-col :span="24">
  			<!--表格数据展示-->
  			<div>
	  			<el-button type="primary" size="small" icon="plus" v-on:click="addFn">新增</el-button>
	  			<el-button type="primary" size="small" icon="edit" v-on:click="editFn">修改</el-button>
	  			<el-button type="primary" size="small" icon="delete" v-on:click="deleteFn">删除</el-button>
  			</div>
  			<div class="data-list">
				<m-table-complex :tableDatas="tableDatalist" :queryTableDataUrl="urlPaths.queryList" :queryTableDataParms="conditionForm" :tableColumn="tableColumn"
					ref="tableChild" @select-rowidx-handler="handleCurrentChange">
				</m-table-complex>
			</div>
  		</el-col>
  		<el-col :span="24">
  			<!--新增和修改 dialog-->
			<el-dialog :title="dialogTitle" v-if="dialogVisible" :visible.sync="dialogVisible">
				<el-form :model="configform" ref="configform" :rules="rules" label-width="100px">
					<m-dialog-form-item v-for="column in tableColumn" :configform="configform" :columnInfo="column"
						:uploadImgAttr="uploadDatas">
					</m-dialog-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="closeDialog">取 消</el-button>
					<el-button type="primary" @click="saveSubmit">确 定</el-button>
				</div>
			</el-dialog>
		</el-col>
	</el-row>
</template>
<script>
	import {
		Message
	} from 'element-ui';
	import store from '../../store'
	export default {
		components:{},
		data() {
			return {
				tableDatalist:[],
				conditionForm: { //查询过滤的条件
					name:''
				},
				tableColumn:[ 
					{
						columnName:'id', //主键字段，修改和删除的依赖列
						columnLabel:'ID',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:false, //是否在新增或修改对话框中显示该属性字段
					},
					{
						columnName:'name',
						columnLabel:'品牌名',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true, //是否在新增或修改对话框中显示该属性字段
					},
					{
						columnName:'simpleDesc',
						columnLabel:'简短描述',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'sortOrder',
						columnLabel:'排序',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'isShow',
						columnLabel:'是否显示',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'floorPrice',
						columnLabel:'底价',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'listPicUrl',
						columnLabel:'列表图片地址750*240',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isPicColumn:true,
						isMulUploadPic:false
					},
					{
						columnName:'picUrl',
						columnLabel:'图片地址760*484',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isPicColumn:true
					},
					{
						columnName:'appListPicUrl',
						columnLabel:'app列表图片地址750*240',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:false,
						isPicColumn:true
					},
					{
						columnName:'isNew',
						columnLabel:'是否新品牌',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'newPicUrl',
						columnLabel:'新品图片地址760*484',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:false,
						isPicColumn:true
					},
					{
						columnName:'newSortOrder',
						columnLabel:'新品排序顺序',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					}
				],
				urlPaths:{
					queryList:'brand/listAll',
					delByKey:'brand/delete',
					insert:'brand/add',
					updateByKey:'brand/update',
				},
				configform: {},
				index: '',
				dialogVisible: false,
				dialogTitle:'新增',
				rules: {},
				currentRow: null,
//				tableData.slice((pagination.currentPage-1)*pagination.pageSize,pagination.currentPage*pagination.pageSize)
				uploadImgs: {},
				uploadDatas:{
					clazz:'brand',
					columnName:''
				}
			}
		}, 
		computed:{
		},
		beforeCreate(){
    	},
    	mounted() {
    		this.initData();
		},
		methods: {
			initData(){
			},
			queryTableData(){
				this.$refs.tableChild.initData();
			},
			/*选择一条数据*/
			handleCurrentChange(val) {
				if(!val) {
					return
				}
				this.currentRow = val;
				this.index = val.index+''
				console.log('this.currentRow ',this.currentRow);
				console.log('this.index ',this.index);
			},
			/*新增*/
			addFn(){
				this.dialogVisible = true;
				this.dialogTitle='新增';
				/*if(this.$refs['configform']!=undefined) {
					this.$refs['configform'].resetFields();
				}*/
				this.setFormData();
			},
			/*编辑*/
			editFn(){
				if(this.index) {
					this.dialogVisible = true;
					this.dialogTitle='编辑';
					this.setFormData();
				}else{
					Message({
						message: '请选择一条进行操作！',
						type: 'warning'
					});
				}
			},
			setFormData(){
				var form ={};
				for(let col of this.tableColumn){
					if (this.dialogTitle=='新增'){
						if (col.defaultValue != undefined && col.defaultValue!=''){
							form[col.columnName] = col.defaultValue;
						} else {
							form[col.columnName] = '';
						}
					} else {
						form[col.columnName] = this.currentRow[col.columnName];
					}
				}
				this.configform = form;
			},
			/*删除*/
			deleteFn(){
				if(this.index) {
					this.$confirm('你确定要删除吗？', '删除提示', {
			          	confirmButtonText: '确定',
			          	cancelButtonText: '取消',
			          	type: 'warning'
			        }).then(() => {
			        	//调删除接口
			        	this.$httpExt().get(this.urlPaths.delByKey, {
							id:this.currentRow.id
						}).then((response) => {
//							Message({
//								message: response.msg,
//								type: 'success'
//							});
							this.queryTableData();
						},(response) => {
							Message({
								message: '服务器请求失败，请重试！',
								type: 'warning'
							});
						});
			        });
				}else{
					Message({
						message: '请选择一条进行操作！',
						type: 'warning'
					});
				}
			},
			/*提交*/
			saveSubmit(){				
				var that = this;
				if(this.dialogTitle == '新增') {
//							that.configform.id = '';
					var url = this.urlPaths.insert;
				} else {
					var url = this.urlPaths.updateByKey;
				}
				console.log(url);
				console.log(that.configform);
				this.$httpExt().post(url,that.configform).then((response) => {
					if(response.succ == 'ok') {
						that.queryTableData();
						that.dialogVisible = false;
					}
				}, (data) => {
					Message({
						message: data.msg ? data.msg : '服务器请求失败，请重试！',
						type: 'warning'
					});
				});
			},
			closeDialog(){
				this.dialogVisible = false;
			},
			handleReset() {
				this.newlyFormVisible = false
			},
	    }
	}
</script>
<style>
.fl{
	float: left;
}
.brand a{
	color: #20a0ff;
	text-decoration: none;
}

</style>